﻿using System.Threading;
using System.Threading.Tasks;
using ESRI.ArcGIS.Client.Geometry;
using ESRI.ArcGIS.Client.Tasks;

namespace ESRI.ArcGIS.Client.Async
{
    public static partial class ComputeHistogramsTaskExtensions
    {
        #region ComputeHistogramsTaskAsync

        /// <summary>
        /// Asynchronous method that computes the histograms for an image server.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="envelope">The envelope geometry filter.</param>
        /// <param name="parameter">The compute histograms parameter.</param>
        /// <returns></returns>
        public static Task<ComputeHistogramsTaskResult> ComputeHistogramsTaskAsync(this ComputeHistogramsTask task,
            Envelope envelope,
            ComputeHistogramsParameter parameter)
        {
            return ComputeHistogramsTaskAsync(task, envelope, parameter, CancellationToken.None);
        }

        /// <summary>
        /// Asynchronous method that computes the histograms for an image server.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="envelope">The envelope geometry filter.</param>
        /// <param name="parameter">The compute histograms parameter.</param>
        /// <param name="cancellationToken">Cancellation token for cancelling task.</param>
        /// <returns></returns>
        public static Task<ComputeHistogramsTaskResult> ComputeHistogramsTaskAsync(this ComputeHistogramsTask task,
            Envelope envelope,
            ComputeHistogramsParameter parameter, CancellationToken cancellationToken)
        {
            return task.HandleTask<ComputeHistogramsTask, ComputeHistogramsEventArgs, ComputeHistogramsTaskResult>(
                t => t.ComputeHistogramsAsync(envelope, parameter),
                (t, e) => t.ComputeHistogramsCompleted += e,
                (t, e) => t.ComputeHistogramsCompleted -= e,
                e => new ComputeHistogramsTaskResult(e),
                cancellationToken);
        }

        /// <summary>
        /// Asynchronous method that computes the histograms for an image server.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="polygon">The polygon geometry filter.</param>
        /// <param name="parameter">The compute histograms parameter.</param>
        /// <returns></returns>
        public static Task<ComputeHistogramsTaskResult> ComputeHistogramsTaskAsync(this ComputeHistogramsTask task,
            Polygon polygon,
            ComputeHistogramsParameter parameter)
        {
            return ComputeHistogramsTaskAsync(task, polygon, parameter, CancellationToken.None);
        }

        /// <summary>
        /// Asynchronous method that computes the histograms for an image server.
        /// </summary>
        /// <param name="task"></param>
        /// <param name="polygon">The polygon geometry filter.</param>
        /// <param name="parameter">The compute histograms parameter.</param>
        /// <param name="cancellationToken">Cancellation token for cancelling task.</param>
        /// <returns></returns>
        public static Task<ComputeHistogramsTaskResult> ComputeHistogramsTaskAsync(this ComputeHistogramsTask task,
            Polygon polygon,
            ComputeHistogramsParameter parameter, CancellationToken cancellationToken)
        {
            return task.HandleTask<ComputeHistogramsTask, ComputeHistogramsEventArgs, ComputeHistogramsTaskResult>(
                t => t.ComputeHistogramsAsync(polygon, parameter),
                (t, e) => t.ComputeHistogramsCompleted += e,
                (t, e) => t.ComputeHistogramsCompleted -= e,
                e => new ComputeHistogramsTaskResult(e),
                cancellationToken);
        }

        #endregion
    }
}

